<?php

namespace App\Http\Controllers\Webdisk;

use App\Http\Controllers\Controller;
use App\Webdisk\Model\User;
use App\Webdisk\SecurityUtils;
use Illuminate\Http\Request;

use function App\Webdisk\makeRes;

class UserController extends Controller
{
    function login(Request $req){
        $req->validate([
            'user'=>['required'],
            'pwd'=>['required']
        ]);
        $entry = User::getByUser($_POST['user']);
        if(!$entry){
            $res = makeRes(-1,null,"用户不存在");
        }else if(SecurityUtils::hashPassword($_POST['pwd']) != $entry['pwd']) {
            $res = makeRes(-1,null,"用户名或密码错误");
        }else{
            setcookie("utoken",$entry['token'],0,'/');
            $entry['last_login'] = time();
            $entry->save();
            $res = makeRes();
        }
        return $res;
    }

    function getInfo(Request $req){
        $data = $req->attributes->get("user")->toArray();
        unset($data['pwd']);
        unset($data['token']);
        return makeRes(1,$data);
    }
}
